import { RequestHandler } from 'express';
import { authController } from '../controllers/authController';
import { userController } from '../controllers/userController';
import { authorize } from '../middleware/roleAuth';
import { asyncHandler } from '../middleware/asyncHandler';
import { validateUserOperation } from '../middleware/validators/userValidator';
import { 
  createTypedRouter, 
  RouterType, 
  asRequestHandler, 
  chainMiddleware 
} from '../types/routes';

const router: RouterType = createTypedRouter();

// Convert controller methods to properly typed request handlers
const handlers = {
  login: asRequestHandler(asyncHandler(authController.login)),
  register: asRequestHandler(asyncHandler(authController.register)),
  getUsers: asRequestHandler(asyncHandler(userController.getUsers)),
  getUserById: asRequestHandler(asyncHandler(userController.getUserById)),
  createUser: asRequestHandler(asyncHandler(userController.createUser)),
  updateUser: asRequestHandler(asyncHandler(userController.updateUser)),
  deleteUser: asRequestHandler(asyncHandler(userController.deleteUser)),
  updateManyUsers: asRequestHandler(asyncHandler(userController.updateManyUsers)),
  searchUsers: asRequestHandler(asyncHandler(userController.searchUsers))
};

// Convert authorization middleware to request handlers
const auth = {
  admin: asRequestHandler(authorize.asAdmin),
  teacher: asRequestHandler(authorize.asTeacher),
  readUser: asRequestHandler(authorize.withPermission('read', 'user')),
  updateUser: asRequestHandler(authorize.withPermission('update', 'user')),
  manageUser: asRequestHandler(authorize.toManage('user')),
  readOnly: asRequestHandler(authorize.toRead('user'))
};

// Public routes
router.post('/login', ...validateUserOperation.creation, handlers.login);
router.post('/register', ...validateUserOperation.creation, handlers.register);

// Protected routes
router.get('/', auth.admin, handlers.getUsers);

router.get('/:id', 
  chainMiddleware(
    ...validateUserOperation.id,
    auth.readUser,
    handlers.getUserById
  )
);

router.post('/',
  chainMiddleware(
    auth.admin,
    ...validateUserOperation.creation,
    handlers.createUser
  )
);

router.put('/:id',
  chainMiddleware(
    ...validateUserOperation.id,
    auth.updateUser,
    ...validateUserOperation.update,
    handlers.updateUser
  )
);

router.delete('/:id',
  chainMiddleware(
    ...validateUserOperation.id,
    auth.admin,
    handlers.deleteUser
  )
);

// Batch operations
router.post('/batch',
  chainMiddleware(
    auth.manageUser,
    ...validateUserOperation.batchUpdate,
    handlers.updateManyUsers
  )
);

// Search operations
router.get('/search',
  chainMiddleware(
    auth.readOnly,
    ...validateUserOperation.search,
    handlers.searchUsers
  )
);

// Role-specific routes
router.get('/students',
  chainMiddleware(
    auth.teacher,
    handlers.getUsers
  )
);

router.get('/teachers',
  chainMiddleware(
    auth.admin,
    handlers.getUsers
  )
);

export default router;
